package com.tanhua.dubbo.api.mongo.impl;

import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public PageResult findComments(String movementId, Integer page, Integer pagesize) {
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(2));
        long count = mongoTemplate.count(query, Comment.class);
        query.skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return new PageResult(page.longValue(), pagesize.longValue(), count, comments);
    }

    @Override
    public Integer save(Comment comment) {
        Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
        Movement movement = mongoTemplate.findOne(query, Movement.class);
        comment.setPublishUserId(movement.getUserId());
        mongoTemplate.save(comment);

        Update update = new Update();
        if (comment.getCommentType() == 1) {
            update.inc("likeCount", 1);
        } else if (comment.getCommentType() == 2) {
            update.inc("commentCount", 1);
        } else {
            update.inc("loveCount", 1);
        }

        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);

        Movement newMovement = mongoTemplate.findAndModify(query, update, options, Movement.class);
        return newMovement.statisCount(comment.getCommentType());
    }

    @Override
    public Integer delete(Comment comment) {
        //对comment表进行操作
        Query deleteQuery = new Query(Criteria.where("publishId").is(comment.getPublishId()).and("commentType").is(comment.getCommentType()).and("userId").is(comment.getUserId()));
        mongoTemplate.remove(deleteQuery, Comment.class);

        //对movement表进行操作
        Update update = new Update();
        if (comment.getCommentType() == 1) {
            update.inc("likeCount", -1);
        } else if (comment.getCommentType() == 2) {
            update.inc("commentCount", -1);
        } else {
            update.inc("loveCount", -1);
        }

        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);

        Query movementQuery = new Query(Criteria.where("id").is(comment.getPublishId()));
        Movement newMovement = mongoTemplate.findAndModify(movementQuery, update, options, Movement.class);
        return newMovement.statisCount(comment.getCommentType());
    }

}